package negocio;

import java.io.Serializable;
import java.util.ArrayList;

import java.util.List;

import java.util.Vector;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import beans.IngredienteVO;
import beans.RecetaVO;

@Entity
@Table(name="recetas")
public class Receta implements Serializable 
{
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id_receta")
	private int id_receta;
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
	@JoinColumn(name="id_receta", referencedColumnName="id_receta")
	private List<Ingrediente> ingredientes = new ArrayList<>();
	
	public int getId_receta() {
		return id_receta;
	}

	public void setId_receta(int id_receta) {
		this.id_receta = id_receta;
	}
	
	public List<Ingrediente> getIngredientes() {
		return ingredientes;
	}

	public void setIngredientes(List<Ingrediente> ingredientes) {
		this.ingredientes = ingredientes;
	}
	
	public Receta()
	{
		ingredientes = new ArrayList<Ingrediente>();
	}
	
	public Receta(List<Ingrediente> ingredientes) {
		super();
		this.ingredientes = ingredientes;
	}

	//toString
	@Override
	public String toString() {
		return "Receta [ingredientes=" + ingredientes + "]";
	}
	
	//hashcode and equals
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((ingredientes == null) ? 0 : ingredientes.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) 
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Receta other = (Receta) obj;
		if (ingredientes == null) {
			if (other.ingredientes != null)
				return false;
		} else if (!ingredientes.equals(other.ingredientes))
			return false;
		return true;
	}
	
	public void buildFromVO(RecetaVO recetaVO)
	{
		this.id_receta = recetaVO.getId_receta();
		Vector<Ingrediente> ings = new Vector<Ingrediente>();
		for (int i = 0 ; i < recetaVO.getIngredientes().size() ; i++)
		{
			Ingrediente ingr = new Ingrediente();
			ingr.buildFromVO(recetaVO.getIngredientes().get(i));
			ings.add(ingr);
		}
		
		
		for(Ingrediente i : ings)
			this.ingredientes.add(i);
		
		
	}
	
	public RecetaVO getVO()
	{
		RecetaVO recetaVO = new RecetaVO();
		recetaVO.setId_receta(this.getId_receta());
		ArrayList<IngredienteVO> ingVOs = new ArrayList<IngredienteVO>();
		
		for(Ingrediente i : this.getIngredientes())
		{
			IngredienteVO ingVO=i.getVO();
			ingVOs.add(ingVO);
		}
		
		recetaVO.setIngredientes(ingVOs);
		return recetaVO;
	}
}
